﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvalonDock;
using System.Windows.Forms;
using System.Windows;
namespace LuanVanProject
{
 public   class DocumentContainForViewer: DocumentContent
    {
        System.Windows.Forms.Integration.WindowsFormsHost host;
        Viewer view;
        public string FileName;
        static PPointViewer pptViewer=null;
        static public void ClosePPT()
        {
            if(pptViewer!=null)
            pptViewer.Close();
            
        }
        static  Viewer previousWordControl = null;
        static int numofword = 1;
        public DocumentContainForViewer( Viewer viewer)
        {
            host = new System.Windows.Forms.Integration.WindowsFormsHost();
            host.Child = viewer;
            this.view = viewer;
            this.Content = host;            
        }
        static public DocumentContainForViewer OpenFile(string fullFileName)
        {
            //short file name
           
            string shortFileName = System.IO.Path.GetFileName(fullFileName);
             Viewer viewer=null;
             fullFileName = fullFileName.ToLower();
            if (utility.EqualsExtend(fullFileName, "pptx") || utility.EqualsExtend(fullFileName, "ppt"))
            {
                if (pptViewer != null)
                {
                    pptViewer.Close();
                    pptViewer.MyContainer.Close();
                }
                pptViewer = new PPointViewer(fullFileName);
                viewer = pptViewer;

                 
            }
            if (utility.EqualsExtend(fullFileName, "docx") || utility.EqualsExtend(fullFileName, "doc"))
            {
                viewer = new WordViewer(fullFileName, numofword);
                numofword++;
                if (previousWordControl != null)
                {
                    previousWordControl.Reactive();
                }
                previousWordControl = viewer;
            }
            if (utility.EqualsExtend(fullFileName, "pdf"))
            {
                viewer= new PDFViewer(fullFileName);
            }
            
            if (utility.EqualsExtend(fullFileName, "png") || utility.EqualsExtend(fullFileName, "jpg") || utility.EqualsExtend(fullFileName, "bmp"))
            {
                viewer = new ImageViewer(fullFileName);
            }

            if (viewer != null)
            {
                DocumentContainForViewer doc = new DocumentContainForViewer(viewer);
                if (utility.EqualsExtend(fullFileName, "pptx") || utility.EqualsExtend(fullFileName, "ppt"))
                {
                    doc.Closed += new EventHandler(doc_Closed);
                }
                if (utility.EqualsExtend(fullFileName, "docx") || utility.EqualsExtend(fullFileName, "doc"))
                {
                    doc.Closed+=new EventHandler(doc_ClosedDoc);
                }
                doc.Title ="[Doc]-"+ System.IO.Path.GetFileName( fullFileName);
                doc.SizeChanged += new SizeChangedEventHandler(item_SizeChanged);
                viewer.MyContainer = doc;
                doc.FileName = fullFileName;

                int rs = viewer.Open();
                if(rs >=0)
                return doc;

            }
            return null;
        }

        static void doc_Closed(object sender, EventArgs e)
        {
            ClosePPT();
        }
        static void doc_ClosedDoc(object sender, EventArgs e)
        {
            (sender as DocumentContainForViewer).view.Close();   
        }
        static void item_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DocumentContainForViewer docContent = sender as DocumentContainForViewer;
            if ((docContent.view as WordViewer) != null)
            {
                (docContent.view as WordViewer).Reactive();
            }
        }


    }
}
